from flask import Blueprint, request, render_template, g, redirect, url_for, jsonify
import json, os
from exts import db
from decorators import login_required
from models import MindModel

from flask_jwt_extended import jwt_required, get_jwt_identity

bp = Blueprint("qa", __name__, url_prefix="/")


# http://127.0.0.1:5000
@bp.route("/")
# @login_required
def index():
    # questions = QuestionModel.query.order_by(QuestionModel.create_time.desc()).all()
    return render_template("index.html")
    # return "hello, world"
    
    
@bp.route("/mindmap", methods=['GET', 'POST'])
@jwt_required()
def mindmap():
    user = get_jwt_identity()
    print(user)
    return "hello"
    if request.method == 'GET':
        return jsonify({'success': True, 'email': 'this is get'})
    else:
        return jsonify({'success': True, 'email': 'this is post'})
    
    
@bp.route("/update", methods=['POST'])
@jwt_required()
def update():
    mind_info = request.get_json()
    mind_json = json.dumps(mind_info)
    # print(type(mind_json)) 
    email = get_jwt_identity()
    mind = MindModel.query.filter_by(email=email).first()
    if not mind:
        print("初次更新")
        mind = MindModel(email=email, mind=mind_json)
        db.session.add(mind)
        db.session.commit()
        
        return jsonify({'success': True, 'message': "bingo"})
    else:
        mind.mind = mind_json
        db.session.add(mind)
        db.session.commit()
        print("迭代更新")
    # print(mind_info)
    # print(type(mind_info))
    return jsonify({'success': True})


@bp.route("/inquire", methods=['get'])
@jwt_required()
def inquire():
    email = get_jwt_identity()
    mind = MindModel.query.filter_by(email=email).first()
    print(os.getcwd())
    if not mind:
        print("无mind数据")
        with open("./static/example.json", 'r') as fp:
            mind_data = json.load(fp)
        # print(mind_data)
        # print(type(mind_data))
        # return mind_data
        # return jsonify({'success': True, 'message': "bingo"})
        # return (mind_data)
        return ''
    else:
        print("导出mind数据")
        mind_data = mind.mind
        # print(mind_data)
        
        # return jsonify({'success': True})
        return mind_data


# @bp.route("/qa/public", methods=['GET', 'POST'])
# @login_required
# def public_question():
#     if request.method == 'GET':
#         return render_template("public_question.html")
#     else:
#         form = QuestionForm(request.form)
#         if form.validate():
#             title = form.title.data
#             content = form.content.data
#             question = QuestionModel(title=title, content=content, author=g.user)
#             db.session.add(question)
#             db.session.commit()
#             # todo: 跳转到这篇问答的详情页
#             return redirect("/")
#         else:
#             print(form.errors)
#             return redirect(url_for("qa.public_question"))


# @bp.route("/qa/detail/<qa_id>")
# def qa_detail(qa_id):
#     question = QuestionModel.query.get(qa_id)
#     return render_template("detail.html", question=question)


# # @bp.route("/answer/public", methods=['POST'])
# @bp.post("/answer/public")
# @login_required
# def public_answer():
#     form = AnswerForm(request.form)
#     if form.validate():
#         content = form.content.data
#         question_id = form.question_id.data
#         answer = AnswerModel(content=content, question_id=question_id, author_id=g.user.id)
#         db.session.add(answer)
#         db.session.commit()
#         return redirect(url_for("qa.qa_detail", qa_id=question_id))
#     else:
#         print(form.errors)
#         return redirect(url_for("qa.qa_detail", qa_id=request.form.get("question_id")))


# @bp.route("/search")
# def search():
#     # /search?q=flask
#     # /search/<q>
#     # post, request.form
#     q = request.args.get("q")
#     questions = QuestionModel.query.filter(QuestionModel.title.contains(q)).all()
#     return render_template("index.html", questions=questions)

# url传参
# 邮件发送
# ajax
# orm与数据库
# Jinja2模板
# cookie和session原理
# 搜索

# 前端
# 部署

# 《Flask全栈开发》：Flask基础增强+前端
# 《Flask实战》：Flask+Vue前后端分离的论坛系统，WebSocket实战

# 简单项目：
# 复杂的项目：

